//
// Created by Admin on 2022/5/12.
//

#include "display_monitor.h"



DisplayMonitor::DisplayMonitor()
{
	display_monitors_ = 0;
}

std::vector<DisplaySettings> DisplayMonitor::getDisplayMonitors()
{
	display_settings_vec_.clear();
	EnumDisplayMonitors(NULL, NULL, DisplayMonitor::enumDisplayMonitorCallBack, (LPARAM)this);
	return display_settings_vec_;
}

BOOL DisplayMonitor::enumDisplayMonitorCallBack(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
{
	DisplayMonitor* ctx = (DisplayMonitor*)dwData;
	if (ctx)
	{
		MONITORINFOEX mi;
		ZeroMemory(&mi, sizeof(mi));
		mi.cbSize = sizeof(mi);
		BOOL ret = GetMonitorInfo(hMonitor, &mi);
		if (ret)
		{
			DEVMODE devMode;
			devMode.dmSize = sizeof(devMode);
			ret = EnumDisplaySettings(mi.szDevice, ENUM_CURRENT_SETTINGS, &devMode);
			if (ret)
			{
				DisplaySettings settings;
				if (mi.dwFlags == MONITORINFOF_PRIMARY) {
					settings.isPrimary = true;
				}
				settings.x = devMode.dmPosition.x;
				settings.y = devMode.dmPosition.y;
				settings.width = devMode.dmPelsWidth;
				settings.height = devMode.dmPelsHeight;
				ctx->display_settings_vec_.emplace_back(settings);
			}
		}
		return TRUE;
	}
	return FALSE;
}